﻿using MCMesServer.DBFunctions.DBFuncs;
using MCMesServer.Model;
using MCMesServer.NetConnectFunc;
using MCMesServer.PublicLogInfo;
using MCMesServer.WorkForms;
using MCMesServer.WorkForms.Pages;
using MesDAL.DTO.NetSetting;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MCMesServer
{
    public partial class MainWindow : UIHeaderMainFrame
    {
        public static MainWindow frm_Main;
        public static string ConfigPath = ConfigurationManager.AppSettings["ConfigPath"];//配置文件路径

        #region 网络连接
        public static bool IsConnect = false;
        public static Dictionary<string, string> ServerAddress = new Dictionary<string, string>();  //machinenum-ipaddress
        public static Dictionary<string, IntPtr> ServerLink = new Dictionary<string, IntPtr>();     //machinenum-intptr
        public static TcpServerConnect interServer;
        #endregion

        #region 页面
        public static Frm_MachineStatusShow frm_MachineStatusShow;
        #endregion

        #region 设备
        private string _ip = "";
        private ushort _port = 0;
        #endregion

        public MainWindow()
        {
            InitializeComponent();

            PageInitialization();

            NetClientConnect();
        }

        private void PageInitialization()
        {
            try
            {
                frm_Main = this;

                string ip = ConfigClass.ReadIniData("NetConnection", "ServerAddress", ConfigPath);
                ushort port = ushort.Parse(ConfigClass.ReadIniData("NetConnection", "ServerPort", ConfigPath));
                _ip = ip;
                _port = port;

                string[] pages = ConfigurationManager.AppSettings["UsePage"].Split(',');

                for (int i = 0; i < pages.Length; i++)
                {
                    if (pages[i] == "")
                    {
                        continue;
                    }

                    if (pages[i] == "MachineStatusShow")
                    {
                        frm_MachineStatusShow = new Frm_MachineStatusShow(ServerAddress, ServerLink);
                        AddPage(frm_MachineStatusShow, 1001);
                        Header.Nodes.Add("设备状态显示");
                        Header.SetNodePageIndex(Header.Nodes[0], 1001);
                    }
                }
                DivideEquipNetSetting();
                ReadProcessSheetName();
                Header.SelectedIndex = 0;
            }
            catch (Exception ex)
            {
                PublicLogInfoFunc.ShowError("初始化", $"ReadDepartmentConfig:{ex.ToString()}");
                Environment.Exit(1);
            }
        }

        /// <summary>
        /// 记录数据库中各个设备ip地址
        /// </summary>
        /// <returns></returns>
        private async void DivideEquipNetSetting()
        {
            List<EquipNetSettingDTO> equipNetSettingList = await EquipNetSettingInfoDBFunc.GetEquipNetSettingInfoList();
            foreach (var item in equipNetSettingList)
            {
                if (ServerAddress.ContainsKey(item.EquipmentNum))
                {
                    ServerAddress[item.EquipmentNum] = item.IpConfig;
                }
                else
                {
                    ServerAddress.Add(item.EquipmentNum, item.IpConfig);
                }

                if (!ServerLink.ContainsKey(item.EquipmentNum))
                {
                    ServerLink.Add(item.EquipmentNum, IntPtr.Zero);
                }
            }
        }

        /// <summary>
        /// 获取工序名
        /// </summary>
        private void ReadProcessSheetName()
        {
            string processStr = ConfigClass.ReadIniData("ProcessSheet", "ProcessName", ConfigPath);

        }

        private void NetClientConnect()
        {
            interServer = new TcpServerConnect(_ip, _port);
            interServer.StartConnect();
        }
    }
}
